
import rcontrolutil
import rcontrol
import Tkinter

#like button, but no icon support (easy to add if desired)

class RCheckbox(rcontrol.RControl):
	"""
	Properties:
	
	*checked* - Represents whether the box is checked.
	*text* - Text of the checkbox. The & character will be shown as an alt-key.
	*onclick* - Function. Command that is called upon click. None represents nothing bound.
	*enabled* - True or False. If False, the checkbox is disabled.
	*height* - Integer, where one unit is the height of a line of text.
	*width* - Integer, where one unit is the width of a char.
	*borderstyle* - Border style. One of 'sunken','raised','groove','ridge','flat'. Default is 'raised'. Set to 'sunken' and it will appear pressed.
	*borderwidth* - Integer, defaults to 1 or 2.
	*font* - Set font of checkbox text. Standard tkinter font format, such as ('Times New Roman', 12, 'normal')
	*anchor* - Set where in the checkbox the text (or image) should be located. Provide one of the strings 'center','top','topright','right','bottomright','bottom','bottomleft','left','topleft'. Default is center.
	*bgcolor* - Background color of the checkbox. RGB tuple, such as (255,0,0) for pure red.
	*color* - Foreground color of the checkbox, to use for text. RGB tuple.
	*wrapwidth* - Set when a checkbox's text should be wrapped into multiple lines, in screen units. Default is 0 (no wrapping).
	*activebgcolor* - Background color of the checkbox when the checkbox is active. RGB tuple.
	*activecolor* - Foreground color of the checkbox when the checkbox is active. RGB tuple.
	*alignment* - Set alignment of text when there are multiple lines. One of 'left','right','center'.
	"""
	
	_basecontrol = None
	_var = None
	
	def __init__(self, strLabel=None, parent=None, **options):
		self._newWidgetManual(parent, Tkinter.Checkbutton)
		
		self._var = Tkinter.IntVar()
		self._basecontrol.config(variable=self._var, offvalue=0, onvalue=1)
		#could also have a 'button style' checkbox with indicatorOn, but this isn't a general setting.
		
		if strLabel!=None: self.text = strLabel
		self.set_many(**options)
	
	def call(self):
		"""Toggles checkbox, also calls the callback associated."""
		self._basecontrol.invoke()
	
	
	
	def property_checked_get(self):
		return self._var.get()!=0
		
	def property_checked_set(self, value):
		if value: self._basecontrol.select()
		else: self._basecontrol.deselect()
	
	#In tkinter, if you set the 'command' to None, it doesn't remove the binding. this fixes that.
	def property_onclick_get(self): #convert '' or 0 to None
		if not self._basecontrol['command']: return None
		else: return self._basecontrol['command']
	def property_onclick_set(self, value):
		if value: self._basecontrol['command'] = value
		else: self._basecontrol['command'] = ''
	
	def property_text_get(self):
		return rcontrolutil.underlinePositionToString(
			self._basecontrol['text'], self._basecontrol['underline'])
	
	# If given a string '&hello', underline the h.
	def property_text_set(self,value):
		text, nIndex = rcontrolutil.stringToUnderlinePosition(value) 
		self._basecontrol.config(text=text,underline=nIndex)
		
	def property_enabled_get(self):
		if self._basecontrol['state']==Tkinter.DISABLED: return False
		else: return True

	def property_enabled_set(self, value):
		if value: self._basecontrol['state'] = Tkinter.NORMAL
		else: self._basecontrol['state'] = Tkinter.DISABLED
			
		
	


	_properties = {
	
		'checked': (property_checked_get, property_checked_set),
		'text': (property_text_get, property_text_set),
	
		'onclick': (property_onclick_get, property_onclick_set),
		'enabled': (property_enabled_get, property_enabled_set),
		'width': 'width',
		'height': 'height',
		
		'borderwidth': 'borderwidth',
		'borderstyle': ('relief',rcontrolutil.mapBorderstyle),
		'font': 'font',
		'wrapwidth': 'wraplength',
		'anchor': ('anchor', rcontrolutil.mapAnchor),
		'alignment': ('justify', rcontrolutil.mapAlignment),
		'bgcolor': ('background',rcontrolutil.mapColor),
		'color': ('foreground',rcontrolutil.mapColor),
		'activebgcolor': ('activebackground',rcontrolutil.mapColor),
		'activecolor': ('activeforeground',rcontrolutil.mapColor),
	
	
		}




